﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Util;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Util.Extension;

namespace AntUnion.FrameWork.Web.Areas.Console.Controllers
{
    [Area("Console")]
    public class CRMWireTransferController : BaseController
    {
        private readonly IWebHostEnvironment _hostingEnv;
        private readonly mtContext _context;
        public CRMWireTransferController(mtContext context, IWebHostEnvironment hostingEnv) : base(context, hostingEnv)
        {
            _context = context;
            _hostingEnv = hostingEnv;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetListJson(int page, int limit, int crmId)
        {
            ListResult res = new ListResult();
            try
            {
                var data = from y in _context.CrmWireTransfers
                           select new
                           {
                               id = y.Id,
                               crmId = y.CrmId,
                               currencyReceipt = y.Currency,
                               payeeNumber = y.AccountNumber,
                               payee = y.Payee,
                               bank = y.Bank,
                               payeeAddress = y.PayeeAddress,
                               bankAddress = y.BankAddress,
                               swiftcode = y.Swiftcode
                           };
                data = data.Where(k => k.crmId == crmId);
                res.Code = 0;
                res.Count = data.Count();
                res.Data = await data.Skip((page - 1) * limit).Take(limit).OrderBy(k => k.id).ToListAsync();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
    }
}
